class @AdminBookListingView extends ListingView
    el: $('#content')

    initialize: ->
        super
        @model.bind("change", @render)
        @model.fetch()

    render: ->
        model = @model
        el = $(@el)

        @Navigate(model.url(), false, true)

        @GetTemplate(SiteMap.Admin.Books.ListingTemplate, (compiledTemplate) ->
            el.html(compiledTemplate(model.toJSON()))
            $('#Filter_AuthorName').autocomplete(
                source: SiteMap.Admin.Shared.GetAuthors
                minLength: 2
                select:(event, ui)->
                    filter = model.get("Filter")
                    filter.AuthorId = ui.item.id
                    filter.AuthorName = ui.item.value
                    model.set("Filter": filter, { silent: true} )
            )
            $('#Filter_CategoryName').autocomplete(
                source: SiteMap.Admin.Shared.GetCategories
                minLength: 2
                select:(event, ui)->
                    filter = model.get("Filter")
                    filter.CategoryId = ui.item.id
                    filter.CategoryName = ui.item.value
                    model.set("Filter": filter, { silent: true} )
            )            
            return
        )

    events:
        "click .search": "search"
        "click .pagination a": "pagination"
        "click th>a": "sort"
        "click .buttons>.create": "create"
        "click .buttons>.delete": "remove"
        "click tbody tr>td:not(.chk)": "edit"
        "change .page-size": "changeSize"
        "click .reset": "reset"
        "hover tr.gridrow": "hoverRow"

    search: (event) ->
        event.preventDefault()
        Loading.Start()
        Messages.WriteMessage('Apply search.')

        filter = @model.get("Filter")
        filter.Name = $("#Filter_Name").val();
        if $('#Filter_AuthorName').val() == ''
            filter.AuthorId = 0
            filter.AuthorName = ''
        if $('#Filter_CategoryName').val() == ''
            filter.CategoryId = 0
            filter.CategoryName = ''

        @model.set("Filter", filter, { silent: true })
        @model.set("Page", 1, { silent: true })      
        @model.fetch()

    hoverRow: (e) ->
        tr = e.currentTarget
        imgSrc = $(tr).data("img")
        $("#BookCover").attr("src", Settings.UploadPath + imgSrc)
        return;

    reset: (event) ->
        event.preventDefault()
        @model.set(
            "Filter"
            {
                Name: ''
                AuthorId: 0
                AuthorName: ''
                CategoryId: 0
                CategoryName: ''
            }
            silent: true
        )
        Loading.Start()
        @model.fetch()

    remove: (event) ->
        event.preventDefault()
        Messages.Clear()
        form = $(@el).find("form.listing")
        if form.find("input:checked").length
            confirmDialog = new BookDeleteConfirm(@)
            confirmDialog.Show("Are you sure?")
        else
            Messages.WriteMessage('Choose one or more books.')

    create: (event) ->
        event.preventDefault()
        Loading.Start()
        @Navigate(SiteMap.Admin.Books.Create, true)

class BookDeleteConfirm extends ConfirmDialog
    constructor: (view) ->
        @view = view
    Ok: ->
        form = $(@view.el).find("form.listing")
        data = form.serialize()
        Loading.Start()
        Messages.WriteMessage('Deleting books.')
        $.post(SiteMap.Admin.Books.Delete, data, (response) =>
            if response.SuccessMessage? && response.SuccessMessage.length
                Messages.WriteSuccessMessage(response.SuccessMessage)
                @view.model.fetch()
            else 
                Messages.WriteErrors(response.Errors)
                Loading.Stop()
        )